﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HappyIntentUpdForm
 * 类型：       控制层
 * 功能说明：   快乐管家-活动计划意愿登记页面
 * 作者：       郭晓庆
 * 创建日期：   2007/09/12
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;

public partial class App_Input_HappyIntentUpdForm : System.Web.UI.Page
{
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HappyIntentUpdForm));
  
    // page load
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!Page.IsPostBack)
        {


            // 保存接收的参数
            ViewState["InpActName"] = Convert.ToString(Context.Items["InpActName"]);
            ViewState["InpActType"] = Convert.ToString(Context.Items["InpActType"]);
            ViewState["InpActStat1"] = Convert.ToString(Context.Items["InpActStat1"]);
            ViewState["InpActStat2"] = Convert.ToString(Context.Items["InpActStat2"]);
            ViewState["InpBeginDate"] = Convert.ToString(Context.Items["InpBeginDate"]);
            ViewState["InpEndDate"] = Convert.ToString(Context.Items["InpEndDate"]);

            ViewState["HidActName"] = Convert.ToString(Context.Items["HidActName"]);
            ViewState["HidActType"] = Convert.ToString(Context.Items["HidActType"]);
            ViewState["HidActStat1"] = Convert.ToString(Context.Items["HidActStat1"]);
            ViewState["HidActStat2"] = Convert.ToString(Context.Items["HidActStat2"]);
            ViewState["HidBeginDate"] = Convert.ToString(Context.Items["HidBeginDate"]);
            ViewState["HidEndDate"] = Convert.ToString(Context.Items["HidEndDate"]);
            ViewState["HidCommand"] = Convert.ToString(Context.Items["HidCommand"]);


            if (Context.Items.Contains("HidPageIndex"))
            {
                ViewState["HidPageIndex"] = Context.Items["HidPageIndex"];
            }

            //接收活动编号
            int actionId = Convert.ToInt32(Context.Items["ACTION_ID"]);
            this.labActinId.Text = Convert.ToString(actionId);




            //检索活动信息
            HappyPlanDao happyPlanDao = new HappyPlanDao();
            HappyPlanBean bean = new HappyPlanBean();


            bean.ActionId = actionId;
            bean = happyPlanDao.FindHappyPlanInfo(bean);

            this.labActionName.Text = bean.ActionName;
            this.labRespPesonName.Text = bean.RespPesonName;
            this.labTypeName.Text = bean.TypeName;
            this.labActionDate.Text = DateUtils.convertToString((DateTime)bean.BespokeDate);
            this.labActionAddress.Text = bean.ActionAddress;

           

            //会员参与活动信息列表(gridview1)
            InitActionMember(actionId);

            //画面控件状态初始化
            ViewState["STATUS"] = "INS";
            this.btnDel.Enabled = false;

            this.txtMemberNo.Attributes.Add("onblur", "showMemberName(this.id,'" + this.txtMemberName.ClientID + "');");
        }
    }

    //会员参与活动信息列表(gridview1)
    private void InitActionMember(int actionId)
    {
        HappyIntentDao dao = new HappyIntentDao();
        ActionMemberBean actionMemberBean = new ActionMemberBean();
        actionMemberBean.ActionId = actionId;
        IList list = dao.FindActionMember(actionMemberBean);
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }

  
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int row = (sender as GridView).SelectedIndex;
        string memberNo = (sender as GridView).DataKeys[row].Value.ToString();


        HappyIntentDao dao = new HappyIntentDao();
        ActionMemberBean bean = new ActionMemberBean();
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        bean.MemberNo = memberNo;
        bean = dao.LoadActionMember(bean);

        this.txtMemberNo.Text = memberNo;
        // this.txtMemberName.Value = bean.NameC;
        this.txtMemberName.Text = bean.NameC;

        this.txtRequest.Text = bean.Request;
        this.txtMemo.Text = bean.Memo;

        this.txtMemberNo.ReadOnly = true;
        this.searchImg.Visible = false;
        this.btnDel.Enabled = true;
        ViewState["STATUS"] = "UPD";
        this.labStatus.Text = "修改 报名情况";

    }
    protected void GridView1_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号

        GridView1.PageIndex = e.NewPageIndex;
        InitActionMember(Convert.ToInt32(this.labActinId.Text));
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //数据验证
        if(this.ComValidator1.DoValidator() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
            return;
        }

        //会员id验证
        if (checkMemberNo() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", this.txtMemberNo.Text);
            return;
        }

        
        HappyIntentDao dao = new HappyIntentDao();
        ActionMemberBean bean = new ActionMemberBean();
        //读取页面信息
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        bean.MemberNo = this.txtMemberNo.Text;
        bean.Request = this.txtRequest.Text;
        bean.Memo = this.txtMemo.Text;

        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        bean.UserId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        //新建
        if("INS".Equals(ViewState["STATUS"]))
        {
            //重复报名验证
            if (checkAction() == false)
            {
                ((Design_MasterPage)this.Master).InfoMessage = "不能重复报名！";

                return;
            }


            dao.InsActionMember(bean);
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();

            this.btnDel.Enabled = true;
        }
        //更新
        else if ("UPD".Equals(ViewState["STATUS"]))
        {
            dao.UpdActionMember(bean);
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();

            
        }

        InitInfo();
        InitActionMember(Convert.ToInt32(this.labActinId.Text));
    }
    private bool checkAction()
    {
        HappyIntentDao dao = new HappyIntentDao();
        ActionMemberBean bean = new ActionMemberBean();
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        bean.MemberNo = this.txtMemberNo.Text;
        if (dao.LoadActionMember(bean) != null)
            return false;
        return true;
    }
    private bool checkMemberNo()
    {
        MemberSrhDao dao = new MemberSrhDao();
        MemberSrhBean bean = new MemberSrhBean();
        bean.MemberNo = this.txtMemberNo.Text;
        if (dao.GetMember(bean) == null)
            return false;
        return true;
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        //删除检查
        if(CheckAppraiseMemo()==false)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0030").ToString().Replace("{0}", "已经被评价"); ;
            return;
        }
        HappyIntentDao dao = new HappyIntentDao();
        ActionMemberBean bean = new ActionMemberBean();
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        bean.MemberNo = this.txtMemberNo.Text;
        dao.DelActionMember(bean);

        ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
        InitInfo();
        InitActionMember(Convert.ToInt32(this.labActinId.Text));
    }

    private bool CheckAppraiseMemo()
    {
        HappyEvaluateDao dao = new HappyEvaluateDao();
        ActionMemberAppraiseBean bean = new ActionMemberAppraiseBean();
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        bean.MemberNo = this.txtMemberNo.Text;
        IList list = dao.CheckMemberAppraise(bean);
        if (list != null && list.Count > 0 ) 
        {
            return false;
        }
        return true;
    }

    private void InitInfo()
    {
        this.btnDel.Enabled = false;
        this.txtMemberNo.Text = "";
        this.txtRequest.Text = "";
        this.txtMemo.Text = "";
        this.txtMemberName.Text = "";
        this.labStatus.Text = "登记 报名情况";
        this.searchImg.Visible = true;
        this.txtMemberNo.ReadOnly = false;
        ViewState["STATUS"] = "INS";
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/HappyPlanSrhForm.aspx";

        // 回传接收的参数       
        Context.Items["InpActName"] = ViewState["InpActName"];
        Context.Items["InpActType"] = ViewState["InpActType"];
        Context.Items["InpActStat1"] = ViewState["InpActStat1"];
        Context.Items["InpActStat2"] = ViewState["InpActStat2"];
        Context.Items["InpBeginDate"] = ViewState["InpBeginDate"];
        Context.Items["InpEndDate"] = ViewState["InpEndDate"];

        Context.Items["HidActName"] = ViewState["HidActName"];
        Context.Items["HidActType"] = ViewState["HidActType"];
        Context.Items["HidActStat1"] = ViewState["HidActStat1"];
        Context.Items["HidActStat2"] = ViewState["HidActStat2"];
        Context.Items["HidBeginDate"] = ViewState["HidBeginDate"];
        Context.Items["HidEndDate"] = ViewState["HidEndDate"];
        Context.Items["HidCommand"] = ViewState["HidCommand"];
        Context.Items["HidPageIndex"] = ViewState["HidPageIndex"];

        ControllerUtils.Transfer(Context, url, true);    
    }

}
